package com.edu.ahm.thread;
/*
* 观察测试线程的状态
* */
public class ThreadState {
    /*
    *  新生 NEW
    *  就绪  sleep() 后 TIMED_WAITING
    *  运行 RUNNABLE
    *  阻塞
    * 结束（死亡）TERMINATED
    * */

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(
                ()->{
                    for (int i = 0; i < 5; i++) {
                        try {
                            Thread.sleep(1000); // 每跑一次睡一秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("//////////");
                    }
                }
        );

        // 观察状态
        Thread.State state = thread.getState();
        System.out.println(state); // NEW

        // 启动线程
        thread.start();
        state = thread.getState();
        System.out.println(state); // RUNNABLE

        while (state != Thread.State.TERMINATED){
            // 只要线程不中止 就一直输出状态
            Thread.sleep(100);
            state = thread.getState();
            System.out.println(state);
        }
        // 死亡之后的 teminated 的线程就不能再次启动了
//        thread.start();
    }



}
